import { Component } from "@angular/core";
import { NavController, NavParams, ToastController } from "ionic-angular";
import { RepairInfoBean } from "./bean/repair-info.bean";
import { HttpService } from "../../providers/http.service";
import * as moment from "moment";
import { Storage } from "@ionic/storage";
import { HomePage } from "./../home/home";
import { CommonFunc } from "../../providers/commonFunc";
@Component({
  selector: "page-repair-info",
  templateUrl: "repair-info.html"
})
export class RepairInfoPage {
  private repairInfoBean: RepairInfoBean = new RepairInfoBean();
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public httpService: HttpService,
    private storage: Storage,
    private toast: ToastController,
    private commonFunc: CommonFunc
  ) {
    storage.get("user").then(val => {
      //alert(JSON.stringify(val));
      //获取当前登录人的信息
      // 设置维修人
      this.repairInfoBean.repairUserId = val.id;
      this.repairInfoBean.repairPartyId = val.partyId;
    });
    this.repairInfoBean.reportDate = this.navParams.get("applyTime");
    this.repairInfoBean.applyRepairId = this.navParams.get("id");
  }

  send() {
    //设置日期
    if (this.repairInfoBean.reportDate) {
      this.repairInfoBean.reportDate = this.repairInfoBean.reportDate
        .replace(/-/g, "")
        .replace(/T/g, "")
        .replace(/Z/g, "")
        .replace(/:/g, "")
        .replace(" ", "");
    } else {
      let tt = this.toast.create({
        message: "请选择报修时间",
        duration: 2000,
        position: "top"
      });
      tt.present();
      return;
    }
    this.repairInfoBean.resolveDate = moment(new Date()).format(
      "YYYYMMDDHHmmss"
    );
    //设置电梯{elevatorType:this.elevatorType,elevatorNum: this.elevatorNum,elevatorId:this.elevatorId}
    this.repairInfoBean.elevatorId = this.navParams.get("elevatorId");
    this.repairInfoBean.elevatorNum = this.navParams.get("elevatorNum");

    let _tt = this;
    this.httpService
      .addRepairInfoElevator(this.repairInfoBean)
      .subscribe(data => {
        if (data.status == 200) {
          let para = Object.assign({}, _tt.navParams.data);
          para["applyTime"] = para["applyTime"]
            .replace(/-/g, "")
            .replace(/T/g, "")
            .replace(/Z/g, "")
            .replace(/:/g, "")
            .replace(" ", "");
          para["dealTime"] = moment(new Date()).format("YYYYMMDDHHmmss");
          para["dealStatus"] = "1";
          para["repairTime"] = para["repairTime"]
            .replace(/-/g, "")
            .replace(/T/g, "")
            .replace(/Z/g, "")
            .replace(/:/g, "")
            .replace(" ", "");

          this.httpService.updateRepairApply(para).subscribe(d => {
            if (d.status == 200) {
              let tt = this.toast.create({
                message: "提交成功",
                duration: 2000,
                position: "top"
              });
              tt.present().then(() => {
                this.navCtrl.setRoot(HomePage).then(() => {
                  this.navCtrl
                    .getActive()
                    .getNav()
                    .remove(0);
                  //场景埋点
                  if (data.data && data.data.status == 1) {
                    this.storage.get(data.data.ext2 + this.repairInfoBean.repairUserId + this.repairInfoBean.elevatorNum).then(dt => {
                      let shownum: number = 0;
                      if (dt) {
                        shownum = dt.shownum;
                      }
                      if (data.data.eventAction == 1) { //nothing to do

                      }
                      if (data.data.eventAction == 2) {
                        if (data.data.ext1 > shownum) {
                          let totst = this.commonFunc.showToast({
                            message: "恭喜您获得：" + data.data.thing + " 分",
                            duration: 3000,
                            position: "top"
                          });
                          totst.present();
                          this.storage.set(data.data.ext2 + this.repairInfoBean.repairUserId + this.repairInfoBean.elevatorNum, { shownum: ++shownum });
                        }

                      }
                      if (data.data.eventAction == 3) {
                        if (data.data.ext1 > shownum) {
                          this.commonFunc.openUrl(data.data.thing);
                          this.storage.set(data.data.ext2 + this.repairInfoBean.repairUserId + this.repairInfoBean.elevatorNum, { shownum: ++shownum });
                        }
                      }
                      if (data.data.eventAction == 4) {
                        if (data.data.ext1 > shownum) {
                          this.navCtrl.push(data.data.thing, {
                            themeData: data.data,
                            elevatorNum: this.repairInfoBean.elevatorNum
                          });
                        }
                      }
                    }, error => {

                    })
                  }
                });
              });
            }
          });
        }
      });
  }

  getImgUrl(param) {
    this.repairInfoBean.maintSignUrl = param.imageDate;
  }
}
